<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title>
            <?php echo ($title); ?>
        </title>
        <script type="text/javascript" src="/task/laybbs/Public/jquery-3.1.1.min.js"></script>
        <script src="/task/laybbs/Public/layui.js"></script>
        <link rel="stylesheet" href="//at.alicdn.com/t/font_001z27klwvobt9.css">
        <link rel="stylesheet" href="/task/laybbs/Public/css/layui.css">
        <link rel="stylesheet" href="/task/laybbs/Public/css/global.css">
        <link rel="shortcut icon" href="/task/laybbs/Public/images/fav.ico" type="image/x-icon"/> 
        <script type="text/javascript">
            $(function () {
                $('#logout-btn').click(function () {
                    layui.use('layer', function(){
                    var layer = layui.layer;
                                 
                    $.ajax({
                        url: "<?php echo U('home/ucenter/logout');?>",
                        type: 'POST',
                        dataType: 'json',
                    })
                    .done(function(response) {
                        if(response.info){
                            layer.msg(response.info, {icon: 6});
                            location.href = "<?php echo U('home/index/index');?>";
                        }else{
                            layer.msg('服务器出错', {icon: 5});
                        }
                    })
                    .fail(function() {
                        console.log("error");
                    });
                    }); 
                });
            });
        </script>
    </head>
    <body <?php echo ($body_tag); ?>>
        <div class="header">
            <div class="main">
                <a class="logo" href="<?php echo U('home/index/index');?>" title="Fly">
                    Fly社区
                </a>
                <div class="nav">
                    <a href="<?php echo U('home/question/qlistall');?>">
                        <i class="iconfont icon-wenda">
                        </i>
                        问答
                    </a>
                    <a href="http://fly.layui.com/case/2016/">
                        <i class="iconfont icon-iconmingxinganli" style="top: 2px;">
                        </i>
                        案例
                    </a>
                    <a href="http://www.layui.com/">
                        <i class="iconfont icon-ui">
                        </i>
                        框架
                    </a>
                </div>
                <?php
 if (!$_SESSION['nickname']) { ?>
                    <div class="nav-user">
                    <a class="unlogin" href="<?php echo U('home/login/index');?>">
                        <i class="iconfont icon-touxiang">
                        </i>
                    </a>
                    <span>
                        <a class="login-btn" href="<?php echo U('home/login/index');?>">
                            登入
                        </a>
                        <a class="reg-btn" href="<?php echo U('home/reg/index');?>">
                            注册
                        </a>
                    </span>
                    <p class="out-login">
                        <a href="<?php echo U('/home/login/qqlogin');?>" onclick="layer.msg('正在通过QQ登入', {icon:16, shade: 0.1, time:0})"
                        class="iconfont icon-qq" title="QQ登入">
                        </a>
                        <a href="<?php echo U('/home/login/wblogin');?>" onclick="layer.msg('正在通过微博登入', {icon:16, shade: 0.1, time:0})"
                        class="iconfont icon-weibo" title="微博登入">
                        </a>
                    </p>
                    </div>
                <?php
 } else { ?>
                    <div class="nav-user">
                        <a href="<?php echo U('home/ucenter/index');?>" class="avatar">
                            <img src="/task/laybbs/<?php echo $_SESSION[avatar];?>" id='avatar-pic'>
                            <cite>
                                <?php echo $_SESSION['nickname'] ?>
                            </cite>
                        </a>
                        <div class="nav">
                            <a href="<?php echo U('home/ucenter/setting');?>">
                                <i class="iconfont icon-shezhi">
                                </i>
                                设置
                            </a>
                            <a href="javascript:void(0);" id="logout-btn">
                                <i style="top: 0; font-size: 22px;" class="iconfont icon-tuichu">
                                </i>
                                退了
                            </a>
                        </div>
                    </div>
                <?php } ?>
            </div>
        </div>
        <div class="main layui-clear">
    <div class="wrap">
        <div class="content detail">
            <h1>
                <?php echo ($q_data['title']); ?>
            </h1>
            <div data-id="4281" class="fly-tip fly-detail-hint">
            <?php if($q_data['qext']['top'] != '0') {?>
                <span class="fly-tip-stick">
                    置顶帖
                </span>
            <?php } elseif($q_data['qext']['excellent'] != '0') {?>
                <span class="fly-tip-jing">
                    精帖
                </span>
            <?php } elseif($q_data['qext']['get_great_ans'] != '0') {?>
                <span class="fly-tip-jie">
                    已采纳
                </span>
            <?php } elseif($q_data['qext']['close'] == '0') {?>
                <span>
                    未结贴
                </span>
            <?php } ?>
                <div class="fly-list-hint">
                    <i title="回答" class="iconfont">
                        
                    </i>
                    <?php echo ($q_data['qext']['num_of_ans']); ?>
                    <i title="人气" class="iconfont">
                        
                    </i>
                    <?php echo ($q_data['qext']['views']); ?>
                </div>
            </div>
            <div class="detail-about">
                <a href="<?php echo U('home/ucenter/index',array('uid'=>$u_data['uid']));?>" class="jie-user">
                    <img alt="<?php echo ($u_data['uname']); ?>" src="/task/laybbs/<?php echo ($u_data['uext']['avatar']); ?>">
                    <cite>
                        <?php echo ($u_data['uname']); ?>
                        <em>
                            发布于<?php echo timeFormat($q_data['qtime']);?>
                        </em>
                    </cite>
                </a>
                <div data-id="4281" id="LAY_jieAdmin" class="detail-hits">
                    <span style="color:#FF7200">
                        悬赏：5飞吻
                    </span>
                    <span data-type="add" type="collect" class="layui-btn layui-btn-mini jie-admin">
                        收藏
                    </span>
                </div>
            </div>
            <div style="margin-bottom: 20px;" class="detail-body photos">
                <?php echo htmlspecialchars_decode($q_data['content']);?>
            </div>
            <a name="comment">
            </a>
            <h2 class="page-title">
                热忱回答
                <span>
                    （
                    <em id="jiedaCount">
                        <?php echo ($q_data['qext']['num_of_ans']); ?>
                    </em>
                    ）
                </span>
            </h2>
            <ul id="jieda" class="jieda photos">
                <?php if(!empty($a_data)) { foreach ($a_data as $value) { ?>
                        <li class="jieda-daan" data-id="<?php echo ($value['aid']); ?>">
                            <a name="item-1476470300661">
                            </a>
                            <div class="detail-about detail-about-reply">
                                <a href="<?php echo U('home/ucenter/index',array('uid'=>$value['uid']));?>" class="jie-user">
                                    <img alt="<?php echo ($value['user']['uname']); ?>" src="/task/laybbs/<?php echo ($value['uext']['avatar']); ?>" layer-index="1">
                                    <cite>
                                        <i>
                                            <?php echo ($value['user']['uname']); ?>
                                        </i>
                                    </cite>
                                </a>
                                <div class="detail-hits">
                                    <span>
                                        <?php echo timeFormat($value['atime']);?>
                                    </span>
                                </div>
                                <?php if ($q_data['qext']['get_great_ans'] == $value['aid']) { ?>
                                    <i title="最佳答案" class="iconfont icon-caina"></i>
                                <?php } ?>
                            </div>
                            <div class="detail-body jieda-body">
                                <?php echo htmlspecialchars_decode($value['content']);?>
                            </div>
                            <div class="jieda-reply">
                                <span type="zan" class="jieda-zan zaned">
                                    <i class="iconfont icon-zan"></i>
                                    <em><?php echo ($value['aext']['praise']); ?></em>
                                </span>
                                <span type="reply">
                                    <i class="iconfont icon-svgmoban53"></i>
                                    回复
                                </span>
                                <?php if ($value['uid'] != $_SESSION['uid'] && $_SESSION['uid'] == $q_data['uid'] && $q_data['close'] == 0) { ?>
                                    <div class="jieda-admin">
                                        <span class="jieda-accept" type="accept">采纳</span>
                                    </div>
                                <?php } ?>
                            </div>
                        </li>
                <?php } } else { ?>
                <li class="fly-none">
                    没有任何回答
                </li>
                <?php } ?>
            </ul>
            <div id="laypage-main"></div>
            <div class="layui-form layui-form-pane">
                <form method="post" action="">
                    <div class="layui-form-item layui-form-text">
                        <div class="layui-input-block">
                            <textarea style="height: 150px;" class="layui-textarea fly-editor" placeholder="我要回答"
                            lay-verify="content" required="" name="content" id="L_content"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <input type="hidden" value="4281" name="jid">
                        <button lay-submit="" lay-filter="asubmit" class="layui-btn">
                            提交回答
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="edge">
        <h3 class="page-title">
    最近热帖
</h3>
<ol class="fly-list-one">
    <?php foreach ($viewsRank as $viewsvalue) {?>
    <li>
        <a href="<?php echo U('home/question/qpage', array('qid'=>$viewsvalue['qid']));?>">
            <?php echo $viewsvalue['title']; ?>
        </a>
        <span>
            <i class="iconfont">
                
            </i>
            <?php echo $viewsvalue['views']; ?>
        </span>
    </li>
    <?php } ?>
</ol>
<h3 class="page-title">
    近期热议
</h3>
<ol class="fly-list-one">
    <?php foreach ($ansnumRank as $ansnumvalue) {?>
    <li>
        <a href="<?php echo U('home/question/qpage', array('qid'=>$ansnumvalue['qid']));?>">
            <?php echo $ansnumvalue['title']; ?>
        </a>
        <span>
            <i class="iconfont">
                
            </i>
            <?php echo $ansnumvalue['num_of_ans']; ?>
        </span>
    </li>
    <?php } ?>
</ol>
    </div>
</div>
<script>
// 收藏
$('.jie-admin').click(function(){
    $(this).toggleClass('layui-btn-danger');
    if ($(this).hasClass('layui-btn-danger')) {
        $(this).text('取消收藏');
        $.ajax({
            url: '<?php echo U("home/ucenter/collection");?>',
            type: 'POST',
            dataType: 'json',
            data: {do: 'addCollection',qid: '<?php echo ($q_data["qid"]); ?>'},
        })
        .done(function(response) {
            if (response) {
                console.log(response);
            }
        })
        .fail(function() {
            console.log("error");
        });
    } else {
        $(this).text('收藏');
        $.ajax({
            url: '<?php echo U("home/ucenter/collection");?>',
            type: 'POST',
            dataType: 'json',
            data: {do: 'delCollection',qid: '<?php echo ($q_data["qid"]); ?>'},
        })
        .done(function(response) {
            if (response) {
                console.log(response);
            }
        })
        .fail(function() {
            console.log("error");
        });
    }
});
// 点赞
$('body').on('click','.jieda-zan',function(){
    var aid = $(this).parent().parent().attr('data-id');
    if ($(this).hasClass('zaned')) {
        var zans = parseInt($(this).children('em').text())+1;
        $(this).children('em').text(zans);
        $.ajax({
            url: '<?php echo U("home/answer/praise");?>',
            type: 'POST',
            dataType: 'json',
            data: {do: 'addPraise',praise: zans,aid: aid},
        })
        .done(function(response) {
            if (response) {
                console.log(response);
            }
        })
        .fail(function(response) {
            console.log(response);
        });
    } else {
        var zans = parseInt($(this).children('em').text())-1;
        $(this).children('em').text(zans);
        $.ajax({
            url: '<?php echo U("home/answer/praise");?>',
            type: 'POST',
            dataType: 'json',
            data: {do: 'delPraise',praise: zans,aid: aid},
        })
        .done(function(response) {
            if (response) {
                console.log(response);
            }
        })
        .fail(function(response) {
            console.log(response);
        });
    }
    $(this).toggleClass('zaned');
});
// 采纳最佳答案
$('.jieda-accept').click(function () {
    var aid = $(this).parent().parent().parent().attr('data-id');
    var qid = <?php echo ($q_data['qid']); ?>;
    
    $.ajax({
        url: '<?php echo U("home/answer/greatAns");?>',
        type: 'POST',
        dataType: 'json',
        data: {aid: aid,qid: qid},
    })
    .done(function(response) {
        if (response == 1) {
            layer.msg('操作成功', {icon: 6}, function () {
                location.href = "<?php echo U('home/question/qpage',array('qid'=>$q_data['qid']));?>";
            });
        };
    })
    .fail(function() {
        console.log("error");
    });
    
});

layui.use(['form', 'layedit', 'laypage'],function() {
    var form = layui.form(),
    layer = layui.layer,
    layedit = layui.layedit,
    laypage = layui.laypage;
    var actname = '<?php echo ($actname); ?>';

    layedit.set({
        uploadImage: {
            url: "<?php echo U('home/answer/aimgupload');?>" //接口url
            ,
            type: 'post' //默认post
        }
    });

    //创建一个编辑器
    editIndex = layedit.build('L_content', {
        tool: ['face', 'image', 'link', 'code', 'yulan']
    });

    // 回复
    $('span[type="reply"]').click(function(){
        var userlink = $(this).parent().siblings('.detail-about-reply').children('.jie-user').attr('href');
        var content = '@<a href="'+userlink+'">'+$(this).parent().siblings('.detail-about-reply').children('.jie-user').children('cite').children('i').text()+'</a>&nbsp;';
        content += layedit.getText(editIndex);
        $('#L_content').val(content);
        // 刷新编辑器
        editIndex = layedit.build('L_content', {
            tool: ['face', 'image', 'link', 'code', 'yulan']
        });
    });

    //自定义验证规则
    form.verify({
        content: function(obj) {
            content = layedit.getContent(editIndex);
            if (content.length < 1) {
                return "内容不能为空";
            } else {
                layedit.sync(editIndex);
            }
        }
    });

    //监听答题提交
    form.on('submit(asubmit)',function(data) {
        $('#L_content').val(layedit.getContent(editIndex));
        data.field.qid = <?php echo ($q_data['qid']); ?>;
        var anplus = parseInt($('#jiedaCount').text())+1;
        data.field.num_of_ans = anplus;
        // 刷新编辑器
        editIndex = layedit.build('L_content', {
            tool: ['face', 'image', 'link', 'code', 'yulan']
        });
        $.ajax({
            url: "<?php echo U('home/answer/apost');?>",
            type: 'POST',
            dataType: 'json',
            data: data.field,
        }).done(function(response) {
            if (response.error) {
                if (!response.skip) {
                    layer.msg(response.error, {
                        icon: 5
                    });
                } else {
                    layer.msg(response.error, {
                        icon: 5
                    },function(){
                        location.href = "<?php echo U('home/login/index');?>";
                    });
                }
            } else if (response.info) {
                layer.msg(response.info, {
                    icon: 6
                });
                var str = '<li class="jieda-daan" data-id="'+response.aid+'"><a name="item-1483513264795"></a><div class="detail-about detail-about-reply"><a class="jie-user"href="<?php echo U("home/ucenter/index", array("uid"=>$_SESSION["uid"]));?>"><img src="/task/laybbs/<?php echo ($_SESSION["avatar"]); ?>"alt="<?php echo ($_SESSION["nickname"]); ?>"layer-index="0"><cite><i><?php echo ($_SESSION["nickname"]); ?></i></cite></a><div class="detail-hits"><span>'+response.atime+'</span></div></div><div class="detail-body jieda-body">'+data.field.content+'</div><div class="jieda-reply"><span class="jieda-zan zaned"type="zan"><i class="iconfont icon-zan"></i><em>0</em></span><span type="reply"><i class="iconfont icon-svgmoban53"></i>回复</span><div class="jieda-admin"></div></div></li>';

                if ($('.fly-none')) {
                    $('.fly-none').remove();
                    $('#jieda').append(str);
                    $('#jiedaCount').text(anplus);
                } else {
                    $('#jieda').append(str);
                    $('#jiedaCount').text(anplus);
                }
            }
        }).fail(function() {
            layer.msg("数据传输失败", {
                icon: 5
            });
        });
        return false;
    });
    // 分页
    laypage({
        cont: $('#laypage-main'),
        pages: "<?php echo ($allpages); ?>",
        curr: function() {
            var page = "<?php echo ($nowpage); ?>"; // 当前页(后台获取到的)
            return page ? page: 1; // 返回当前页码值
        } (),
        jump: function(e, first) { //触发分页后的回调(单击页码后)
            if (!first) { //一定要加此判断，否则初始时会无限刷新
                $.ajax({
                    url: "<?php echo U('home/question/ansPage');?>",
                    type: 'POST',
                    dataType: 'json',
                    data: {apage: e.curr, qid: <?php echo ($q_data['qid']); ?>},
                })
                .done(function(response) {
                    var list = response.page_list;
                    var list = list.replace(/ROOT/g,'/task/laybbs');
                    $('#jieda').empty();
                    $('#jieda').append(list);
                })
                .fail(function() {
                    console.log("error");
                });
            }
        }
    });
});
</script>
        <div class="footer">
            <p>
                <a href="http://fly.layui.com/">
                    Fly社区
                </a>
                2016 &copy;
                <a href="http://www.layui.com/">
                    layui.com
                </a>
            </p>
            <p>
                <a href="http://fly.layui.com/jie/3147.html" target="_blank">
                    产品授权
                </a>
                <a href="http://fly.layui.com/jie/4536.html" target="_blank">
                    获取Fly社区模版
                </a>
                <a href="http://fly.layui.com/jie/2461.html" target="_blank">
                    微信公众号
                </a>
            </p>
        </div>
    </body>
</html>